package com.anrongtec.zcpt.fragment.myfocusfragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.anrongtec.zcpt.R;
import com.anrongtec.zcpt.adapter.NewsListViewAdapter;
import com.anrongtec.zcpt.base.BaseFragment;
import com.anrongtec.zcpt.model.News;
import com.anrongtec.zcpt.ui.activity.NewsDetailActivity;
import com.anrongtec.zcpt.ui.activity.myfocus.FocusAdapter;
import com.anrongtec.zcpt.widget.PullToRefreshListView;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

import static com.anrongtec.zcpt.R.id.lv_focus_news;


public class NewFragment extends BaseFragment {

    private TagFlowLayout tf_tag_fragment1;
    private ArrayList<String> tagTitleData1;
    private FocusAdapter adapter;

    private PullToRefreshListView pullToRefreshListView;
    private News news;
    public static List<News> newsDataList = new ArrayList<News>();
    private NewsListViewAdapter newsListViewAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_new, container, false);
        tf_tag_fragment1 = (TagFlowLayout) view.findViewById(R.id.tf_tag_fragment1);
        pullToRefreshListView = (PullToRefreshListView) view.findViewById(lv_focus_news);
        initData();
        testLoadNewsData();
        initEvent();
        return view;
    }

    private void initEvent() {
        newsListViewAdapter = new NewsListViewAdapter(getActivity(), newsDataList,
                R.layout.news_list_item);
        pullToRefreshListView.setAdapter(newsListViewAdapter);
        pullToRefreshListView
                .setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {
                        Intent intent = new Intent(view.getContext(),
                                NewsDetailActivity.class);
                        intent.putExtra("news_id", position);
                        view.getContext().startActivity(intent);
                    }
                });
    }

    private void initData() {
        tagTitleData1 = new ArrayList<>();
        tagTitleData1.add("张三 ");
        tagTitleData1.add("张婷");
        tagTitleData1.add("王国华");
        tagTitleData1.add("王五");
        tagTitleData1.add("冯云");
        for (int i = 1; i <= 20; i++) {
            news = new News("简单的新闻列表事例标题" + i, "Freedom", "2014-1-" + i,
                    12 + i, "新闻内容" + i);
            newsDataList.add(news);
        }
        adapter = new FocusAdapter(tagTitleData1, getActivity());
        tf_tag_fragment1.setAdapter(adapter);
    }

    /**
     * 测试读取新闻列表项中的数据
     */
    private void testLoadNewsData() {
        Log.w("当前newsDataList中新闻数量为", String.valueOf(newsDataList.size()));
        for (int i = 1; i <= newsDataList.size(); i++) {
            News news = newsDataList.get(i - 1);
            Log.i("第" + i + "条新闻标题", news.getTitle());
            Log.i("第" + i + "条新闻作者", news.getAuthor());
            Log.i("第" + i + "条新闻发表日期", news.getPubDate());
            Log.i("第" + i + "条新闻评论数", String.valueOf(news.getCommentCount()));
            Log.i("第" + i + "条新闻内容", news.getBody());
        }
    }


}
